import React,{useState,useCallback} from 'react'
import Son from './Son';

export default function useCallbackDemo() {
    
  //  定义状态
  const [age,setAge] = useState(12);
  const [price,setPrice] = useState(5);

  function changeAge(){    
    setAge(age+1);
  }
 
  // useCallback：
  // 当依赖发送变化时，才会重新定义函数
  const changeCount = useCallback(function(inc){
    setAge(age+inc);
  },[])

  function changePrice(){
    setPrice(price+1);
  }

  return (
    <>
      <div>useCallback</div>
      <p>age:{age}</p>
      <input type="button" value="修改age" onClick={changeAge} />
      <p>price:{price}</p>
      <input type="button" value="修改Price" onClick={changePrice} />
      <Son count={age} changecount={changeCount} />
    </>
  )
}

